package good.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class SocketThread implements Runnable {
	private Socket socket;
	private String data = "";
	private StringBuffer sb;
	private InputStreamReader isr;
	private BufferedReader br;

	public SocketThread(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		System.out.println("good");
		try {
			isr = new InputStreamReader(socket.getInputStream(),"utf-8");
			br = new BufferedReader(isr);
			data = br.readLine();
			sb=new StringBuffer();
			while (data != null) {
				sb.append(data);
				data = br.readLine();
			}
			socket.shutdownInput();
			System.out.println(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			br = null;
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			isr = null;
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				socket = null;
			}
		}

	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public StringBuffer getSb() {
		return sb;
	}

	public void setSb(StringBuffer sb) {
		this.sb = sb;
	}

}
